package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.TrainScheduleDao;
import com.my12306.train.model.entity.MileageEntity;
import com.my12306.train.model.entity.TrainEntity;
import com.my12306.train.model.entity.TrainScheduleEntity;
import com.my12306.train.model.vo.TrainScheduleVo;
import com.my12306.train.service.MileageService;
import com.my12306.train.service.TrainScheduleService;
import com.my12306.train.service.TrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("trainScheduleService")
public class TrainScheduleServiceImpl extends ServiceImpl<TrainScheduleDao, TrainScheduleEntity> implements TrainScheduleService {
    @Autowired
    private TrainService trainService;
    @Autowired
    private MileageService mileageService;
    @Override
    public PageUtils queryTrainSchedulePage(PageVo pageVo) {
        pageVo = pageVo.init();
        List<TrainScheduleEntity> trains = this.baseMapper.queryTrainSchedulePage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        if (trains != null) {
            Integer dataCount = queryTrainScheduleCount(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
            System.out.println("数据总量：" + dataCount);
            return PageUtils.build(pageVo, trains, dataCount);
        }
        return null;    }

    @Override
    public Integer queryTrainScheduleCount(PageVo pageVo, int logicDelete) {
        Integer count = this.baseMapper.queryTrainScheduleCount(pageVo, logicDelete);
        return count;
    }

    @Override
    public Long saveTrainSchedule(TrainScheduleEntity trainSchedule) {
        if (trainSchedule != null) {
            System.out.println(trainSchedule.toString());
            trainSchedule.setId(SnowflakeIdWorker.generateId());
            trainSchedule.setCreateDate(new Date());
            trainSchedule.setUpdateDate(new Date());
            int insert = this.baseMapper.insert(trainSchedule);
            System.out.println("成功添加车次数："+ insert);
        }
        return null;
    }

    @Override
    public void updateTrainScheduleById(TrainScheduleEntity trainSchedule) {
        if (trainSchedule != null) {
            System.out.println(trainSchedule.toString());
            trainSchedule.setUpdateDate(new Date());
            this.baseMapper.updateById(trainSchedule);
        }
    }

    @Override
    @Transactional
    public ResponseData deleteTrainScheduleBatchByIds(List<Long> trainScheduleIds) {
        this.baseMapper.deleteTrainScheduleBatchByIds(trainScheduleIds, CommonConstants.LogicDelete.delete.getValue());
        return ResponseData.ok();
    }

    @Override
    public ResponseData getAddParameters(Long id) {
        List<TrainEntity> trainList = trainService.queryAllTrain(CommonConstants.LogicDelete.no_delet.getValue());
        List<MileageEntity> mileageList = mileageService.queryAllMileage(CommonConstants.LogicDelete.no_delet.getValue());
        TrainScheduleVo trainSchedule = null;
        if (id != null) {
            trainSchedule = queryTrainScheduleVoById(id, CommonConstants.LogicDelete.no_delet.getValue());
        }
        return ResponseData.ok().put("trainList", trainList).put("mileageList", mileageList).put("trainSchedule", trainSchedule);
    }

    @Override
    public TrainScheduleVo queryTrainScheduleVoById(Long id, int logicDelete) {
        if (id != null) {
            return this.baseMapper.queryTrainScheduleVoById(id, logicDelete);
        }
        return null;
    }

    @Override
    public List<TrainScheduleEntity> queryAllTrainSchedule(int logicDelete) {
        return this.baseMapper.queryAllTrainSchedule(logicDelete);
    }

    @Override
    public TrainScheduleEntity queryTrainScheduleByTrainCode(String trainCode, Integer logicDelete) {
        if (trainCode!=null&&logicDelete!=null){
            QueryWrapper<TrainScheduleEntity> qw = new QueryWrapper<>();
            qw.eq("train_code",trainCode).eq("logic_delete",logicDelete);
           return this.baseMapper.selectOne(qw);
        }
        return null;
    }
}
